package mx.gob.imss.reiman.clientes.util;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

/**
 * 
 * @author Alberto Dzul Calvillo
 * @version 1.0
 * 
 */
public class Herramientas {
	public static boolean isNumber(String s) {
		try {
			Long.parseLong(s);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static void escribeXML(String ruta, String contenido) {
		try {
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(ruta), "UTF8"));
			out.write(contenido);
			out.close();
		} catch (UnsupportedEncodingException e) {
		} catch (IOException e) {
		}
	}

	public static void escribeXML(String ruta, String extension,
			String contenido) {
		try {
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(ruta + extension), "UTF8"));
			out.write(contenido);
			out.close();
		} catch (UnsupportedEncodingException e) {
		} catch (IOException e) {
		}
	}

	public static void escribeXML(String ruta, Node node) {
		try {
			OutputStream output = new FileOutputStream(ruta);
			TransformerFactory tfactory = TransformerFactory.newInstance();
			Transformer serializer;
			serializer = tfactory.newTransformer();
			serializer.setOutputProperty(OutputKeys.INDENT, "yes");
			serializer.setOutputProperty(
					"{http://xml.apache.org/xslt}indent-amount", "2");
			// serializer.transform(new DOMSource(node), new
			// StreamResult(System.out));
			serializer.transform(new DOMSource(node), new StreamResult(output));
		} catch (TransformerException e) {
		} catch (FileNotFoundException e) {
		}
	}
}
